import { Bitmap, DisplayObject, GameEngine } from "./engine";


export function add(a: number, b: number) {
    return a + b;
}

const engine = new GameEngine();
const sceneConfig = [
    { type: 'rectangle', properties: { x: 150, y: 50, color: '#ff0000', alpha: 0.5 } },
    { type: 'textfield', properties: { x: 300, color: '#ff0000', text: '王泽', alpha: 0.5 } },
    { type: 'textfield', properties: { x: 300, y: 40, text: '数字媒体技术', alpha: 1 } },
    { type: 'bitmap', id: "bitmap-1", properties: { x: 0, y: 100, source: 'assets/icon.jpg' } },
];

let move: Move;

engine.start(['assets/icon.jpg'], sceneConfig);
engine.onStart = () => {
    const bitmap = engine.getDisplayObject('bitmap-1') as Bitmap;
    move = new Move(bitmap, 60);
}

engine.onUpdate = (duringTime) => {
    move.update(duringTime);
}

engine.onTick = () => {
    // move.tick();
}

class Move {
    private totalTime = 0;
    private initialX: number = 0;
    constructor(private displayObject: DisplayObject, private speedX: number) {
        this.initialX = displayObject.x;
    }

    update(duringTime: number) {
        this.totalTime += duringTime;
        const s0 = this.initialX;
        const v = this.speedX;
        const t = this.totalTime / 1000;
        const s = s0 + v * t;
        this.displayObject.x = s;
    }

    tick() {
        this.displayObject.x += 1;
    }
}
